//
//  XYWelcomeViewController.swift
//  01-新浪微博
//
//  Created by 陈许亚 on 16/3/10.
//  Copyright © 2016年 cxy. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class XYWelcomeViewController: UIViewController {
    
        override func loadView() {
            view = backImageView
        }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setUpUI()
        
    }
//TODO: QEWRWFE,;GFB
    //FIXME:CNSVNSVOEHYR
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        //开始动画
        startAnimation()
    }
    //TODO:
    
    //设置界面UI
    /**
    懒加载子控件
    设置子控件的frame
    
    */
    func setUpUI(){
        
        view.addSubview(iconImageView)
        view.addSubview(welcomeLabel)
        
        iconImageView.snp_makeConstraints { (make) -> Void in
            //backImageView.snp_centerX
            make.centerX.equalTo(view.snp_centerX)
            make.bottom.equalTo(view.snp_bottom).offset(-180)
            make.size.equalTo(CGSize(width: 90, height: 90))
        }
        welcomeLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconImageView.snp_centerX)
            make.top.equalTo(iconImageView.snp_bottom).offset(20)
        }
        
        
        //iconImageView.layer.cornerRadius = (iconImageView.image?.size.height)! / 2
        iconImageView.layer.cornerRadius = 45
        iconImageView.clipsToBounds = true
        iconImageView.layer.borderWidth = 2
        iconImageView.layer.borderColor = UIColor.grayColor().CGColor
        
        //let url = NSURL(string: XYUserAccount.sharedAccount.headImage)
        //用AFNetworking 来加载图片
        //iconImageView.setImageWithURL(<#T##url: NSURL!##NSURL!#>)
        //用SDWebImage来加载图片，夹前缀sd_
        iconImageView.sd_setImageWithURL( XYUserAccount.sharedAccount.headImage)
    }
    
    
    //开始动画
    func startAnimation(){
        
        /**
         Duration:持续时间
         delay:延迟
         usingSpringWithDamping:弹簧系数 0~1，越小越弹
         initialSpringVelocity：加速度 9.8，效果比较好的话设置范围是：弹簧系数 * 10 ~= 加速度
         options：动画的可选项，枚举， OC：中使用‘|’位移枚举，Swift中使用数组 "[]"的形式表示多个枚举选项
         */
        let offSetY = -(UIScreen.mainScreen().bounds.height) + 180
        self.iconImageView.snp_updateConstraints(closure: { (make) -> Void in
            make.bottom.equalTo(self.view.snp_bottom).offset(offSetY)
        })
        
        UIView.animateWithDuration(2.0, delay: 0, usingSpringWithDamping: 0.98, initialSpringVelocity: 9.8, options: [], animations: { () -> Void in
            
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                print("ok")
                //动画完成之后，显示首页
                UIApplication.sharedApplication().keyWindow?.rootViewController = XYMainViewController()
                
        }
    }
    
    
    //MARK:懒加载
    //背景图片
    lazy var backImageView = UIImageView(image: UIImage(named: "ad_background"))
    //用户图标
    lazy var iconImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    //欢迎label
    lazy var welcomeLabel: UILabel = UILabel.labelWith((XYUserAccount.sharedAccount.name ?? "") + "欢迎回来", textColor: UIColor.darkGrayColor(), fontSize: 18)
    //TODO:记得要加label的alpha
    
}
